package com.pinfly.common.util;

import java.util.Iterator;
import java.util.NoSuchElementException;

/**
 * A class that wraps an Iterator to make it immutable. Calls to
 * <code>hasNext</code> and <code>next</code> are passed to the underlying
 * Iterator. Calls to <code>remove</code> throw an
 * <code>UnsupportedOperationException</code>.
 */
public class ImmutableIterator implements Iterator
{
    // The underlying iterator
    private final Iterator m_iterator;

    /**
     * Create an ImmutableIterator
     * 
     * @param iterator The Iterator to base the ImmutableIterator on.
     */
    public ImmutableIterator (Iterator iterator)
    {
        m_iterator = iterator;
    }

    /**
     * Returns true if the iteration has more elements. In other words, returns
     * true if next would return an element rather than throwing an exception.
     * 
     * @return <code>true</code> if the iterator has more elements.
     */
    public boolean hasNext ()
    {
        return m_iterator.hasNext ();
    }

    /**
     * Returns the next element in the interation.
     * 
     * @return the next element in the iteration.
     * @throws NoSuchElementException if the iteration has no more elements
     */
    public Object next () throws NoSuchElementException
    {
        return m_iterator.next ();
    }

    /**
     * Throws an UnsupportedOperationException This iterator is immutable, so
     * all calls to this method throw the exception.
     * 
     * @throws UnsupportedOperationException
     */
    public void remove () throws UnsupportedOperationException
    {
        throw new UnsupportedOperationException ("This iterator does not support remove");
    }
}
